package com.iotproject.eduservice.service.impl;

import com.iotproject.eduservice.client.VodClient;
import com.iotproject.eduservice.entity.EduSection;
import com.iotproject.eduservice.mapper.EduSectionMapper;
import com.iotproject.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-08
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduSectionMapper, EduSection> implements EduVideoService {

    @Autowired
    VodClient vodClient;

    @Override
    public void deleteByCourseId(String courseId) {
        //根据课程Id查询所有小节
        QueryWrapper<EduSection> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id", courseId);
        List<EduSection> eduSections = baseMapper.selectList(eduVideoQueryWrapper);
        //先删小节的视频
        StringBuilder delVodAll = new StringBuilder();
        int len = eduSections.size();
        for (int i = 0; i < len; i++) {
            //得到所有要删小节的视频id
            String videoSourceId = eduSections.get(i).getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {//非空
                delVodAll.append(videoSourceId);
                if (i < len - 1)  {
                    delVodAll.append(",");
                }
            }
        }
        if (!StringUtils.isEmpty(delVodAll))vodClient.deleteAlyiVideo(delVodAll.toString());
        //删除所有小节
        baseMapper.delete(eduVideoQueryWrapper);
    }
}
